<?php
/***************************************************************************
 *                               recent.php
 *                            -------------------
 *   begin                : Saturday, Mar 05, 2005
 *   copyright            : (C) 2005 Xpert
 *   email                : xpert@phpbbguru.net
 *
 *   $Id: recent.php,v 1.0.4 2005/08/03 20:54:00 xpert Exp $
 *
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

/* Config section */
$cfg_ignore_forums = '49, 45, 58, 65, 47, 57, 63, 68'; 	// ids of forums you don't want to display, separated by commas
$cfg_nm_topics = 10;		// number of topics to output
$cfg_max_topic_length = 40; // max topic length, if more, title will be shortened
/* End of config */

define('IN_PHPBB', true);
$phpbb_root_path = './';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);

//
// Let's prevent caching
//
if (!empty($HTTP_SERVER_VARS['SERVER_SOFTWARE']) && strstr($HTTP_SERVER_VARS['SERVER_SOFTWARE'], 'Apache/2'))
{
	header ('Cache-Control: no-cache, pre-check=0, post-check=0');
}
else
{
	header ('Cache-Control: private, pre-check=0, post-check=0, max-age=0');
}
header ('Expires: 0');
header ('Pragma: no-cache');
header ('Content-Type: text/html; charset=windows-1251');

//
// Define censored word matches
//
$orig_word = array();
$replacement_word = array();
obtain_word_list($orig_word, $replacement_word);

//
// Building URL
//
$server_protocol = ($board_config['cookie_secure']) ? 'https://' : 'http://';
$server_name = preg_replace('#^\/?(.*?)\/?$#', '\1', trim($board_config['server_name']));
$server_port = ($board_config['server_port'] <> 80) ? ':' . trim($board_config['server_port']) : '';
$script_name = preg_replace('#^\/?(.*?)\/?$#', '\1', trim($board_config['script_path']));
$script_name = ($script_name == '') ? $script_name : '/' . $script_name;
$board_path = $server_protocol . $server_name . $server_port . $script_name;
$viewtopic_url = $board_path . '/viewtopic.' . $phpEx;

//
// Session management
// I don't want to log user in, so let's trick
//

$sql = "SELECT * FROM " . USERS_TABLE . " WHERE user_id = ". ANONYMOUS;
if ( !($result = $db->sql_query($sql)) )
{
	message_die(CRITICAL_ERROR, 'Could not obtain anonymous data from user table', '', __LINE__, __FILE__, $sql);
}
$userdata = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
init_userprefs($userdata);

//
// Start initial var setup
//
if ( isset($HTTP_GET_VARS[POST_FORUM_URL]) || isset($HTTP_GET_VARS['forum']) )
{
	$forum_id = ( isset($HTTP_GET_VARS[POST_FORUM_URL]) ) ? intval($HTTP_GET_VARS[POST_FORUM_URL]) : intval($HTTP_GET_VARS['forum']);
	$forum_sql = ' t.forum_id = ' . $forum_id;
}
else
{
	$forum_id = '';
	$forum_sql = ' t.forum_id = f.forum_id AND t.forum_id NOT IN (' . $cfg_ignore_forums . ')';
	if ( isset($HTTP_GET_VARS[POST_CAT_URL]) || isset($HTTP_GET_VARS['cat']) )
	{
		$cat_id = ( isset($HTTP_GET_VARS[POST_CAT_URL]) ) ? intval($HTTP_GET_VARS[POST_FORUM_URL]) : intval($HTTP_GET_VARS['cat']);
		$forum_sql .= ' AND f.cat_id = ' . $cat_id;
	}
}

$topic_status_sql = TOPIC_LOCKED. ',' . TOPIC_MOVED;


// Fetching topics of public forums
$sql = "SELECT t.topic_id, t.topic_title, t.topic_last_post_id, t.topic_replies, p.post_time
	FROM " . TOPICS_TABLE . " AS t, " . POSTS_TABLE . " AS p, " . FORUMS_TABLE . " AS f
	WHERE $forum_sql
		AND f.auth_read = " . AUTH_ALL . "
		AND p.post_id = t.topic_last_post_id
		AND t.topic_moved_id = 0
		AND t.topic_status NOT IN (" . $topic_status_sql . ")
                AND f.forum_id NOT IN (45, 58, 63, 69, 47, 70, 74, 75, 83, 84, 85, 106, 107, 113, 142, 143, 76, 77, 78, 79, 80, 86, 87, 88, 89, 90, 118, 126, 127, 128, 129, 130, 134, 119, 120, 121, 122)
		GROUP BY t.topic_id
		ORDER BY p.post_time DESC LIMIT " . $cfg_nm_topics;

if ( !$result = $db->sql_query($sql) )
{
	message_die(GENERAL_ERROR, "Could not fetch recent topics information.", '', __LINE__, __FILE__, $sql);
}

$total_rows = $db->sql_numrows($result);
if ($total_rows > 0)
{
	//
	// Load template
	//
	$template->set_filenames(array(
		'body' => 'recent_body.tpl')
	);

	while ( $row = $db->sql_fetchrow($result) )
	{
		$full_topic_title = $row['topic_title'];
		if ( count($orig_word) )
		{
			$full_topic_title = preg_replace($orig_word, $replacement_word, $full_topic_title);
		}
		$topic_title = $full_topic_title;
		$topic_title = ( strlen($topic_title) > $cfg_max_topic_length ) ? substr($topic_title, 0, $cfg_max_topic_length) . '&hellip;' : $topic_title;
		$topic_replies = ( $row['topic_replies'] > 0) ? '(' . $row['topic_replies'] . ')': '';
		$post_time = create_date($lang['DATE_FORMAT'] . ' h:s', $row['post_time'], $board_config['board_timezone']);

		$template->assign_block_vars('topicrow', array(
			'TOPIC_REPLIES' => $topic_replies,
			'POST_TIME' => $post_time,
			'FULL_TOPIC_TITLE' => str_replace("'", "\'", $full_topic_title),
			'TOPIC_TITLE' => str_replace("'", "\'", $topic_title),
			'U_TOPIC' => $viewtopic_url . '?' . POST_TOPIC_URL . '=' . $row['topic_id'])
		);
	}
	//
	// Output
	//
	$template->pparse('body');
}
$db->sql_freeresult($result);
?>